AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("shared_teams.lua")
 
include('shared.lua')
include('ball.lua')
include('dsplayer.lua')
include('control.lua')
include('round.lua')
include('half.lua')
include('match.lua')

function GM:PlayerSpawn(ply)
	self.BaseClass:PlayerSpawn(ply)
	ply:SetGravity(1)  
	ply:SetMaxHealth(100, true)
	ply:SetWalkSpeed(250)  
	ply:SetRunSpeed(300)
	//ply:ChatPrint("You are on team "..ply:Team().." ("..team.GetName( ply:Team() )..")") 
end

function GM:PlayerLoadout(ply)
	ply:Give("weapon_crowbar")
	ply:Give("weapon_physcannon")
end

function CMD_Test(ply, command, arguments)
	Round.Start()
end

function CMD_JoinTeam(ply, command, arguments)
	for k, v in pairs(DsTeam) do
		if arguments[1] == tostring(v) then
			ply:SetTeam(v)
			ply:Spawn()
			return
		end
	end
	GAMEMODE:PlayerSpawnAsSpectator(ply)
end

function GM:Goal(scoredTeam)
	team.AddScore(scoredTeam, 1)
	PrintMessage(HUD_PRINTTALK, tostring(team.GetScore(DsTeam.Blue)) .. "-" .. tostring(team.GetScore(DsTeam.Red)))
end

concommand.Add("sj_test", CMD_Test)
concommand.Add("sj_join_team", CMD_JoinTeam)

function GM:RoundStart()
	for _, ply in pairs(player.GetAll()) do
		if ply:Team() == DsTeam.Blue or ply:Team() == DsTeam.Red then
			ply:Spawn()
			return
		end
	end
end

function GM:TimeUp()
end

function GM:MatchEnd(winnerTeam)
end

hook.Add("EntityRemoved", "OnEntityRemoved", function(ent)
	if ent:IsPlayer() and ent:IsBallOwner() then
		if ent:IsBallHolder() then
			Ball.TeleportToPlayerPosition(ply)
		end
		Ball.Clear()
	end
end)